<template>
  <div class="container">
    <!-- 1.0头部导航栏 -->
    <div class="info-eait">
      <van-nav-bar
        title="编辑资料"
        left-arrow
        left-text="返回"
        @click-left="$router.go(-1)"
      />
    </div>
    <div class="info-img">
      <van-image
        :src="userInfo.photo"
        width="120px"
        height="120px"
        fit="cover"
        round
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 个人资料 -->
    <div>
      <van-cell-group>
        <van-cell
          title="名称"
          is-link
          to="/eait"
          :value="userInfo.name"
          @click="eaitClick"
        />

        <van-cell
          title="性别"
          is-link
          :value="userInfo.gender ? '女' : '男'"
          @click="showPopup = true"
        />
        <van-cell
          title="生日"
          is-link
          :value="userInfo.birthday"
          @click="
            showBirthday = true;
            currentDate = new Date(userInfo.birthday);
          "
        />
      </van-cell-group>
    </div>

    <!--1. 编辑资料弹框 -->
    <van-dialog
      v-model="showDialog"
      title="修改名称"
      @confirm="patchEait"
      show-cancel-button
    >
      <van-field v-model="newName" ref="name" placeholder="请输入用户名" />
    </van-dialog>

    <!--2.性别选择弹框 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="性别"
        left-text="取消"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="sexClick(0)" />
      <van-cell title="女" is-link @click="sexClick(1)" />
    </van-popup>
    <!-- 3.年龄选择 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = false"
        @confirm="postBirthday"
      />
    </van-popup>

    <!-- 图片裁切 -->
    <div class="Cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120px"
        autoCropHeight="120px"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="confirmPhoto">确认</van-button>
    </div>
  </div>
</template>

<script>
//导入mapState
import { mapState } from "vuex";
//导入接口
import { postUser } from "./user.js";
//导入时间插件
import moment from "moment";
// 导入头像接口
import { postPhoto } from "./user.js";
// 导入裁切组件
import { VueCropper } from "vue-cropper";
export default {
  name: "eait",
  // 注册组件
  components: {
    VueCropper,
  },
  data() {
    return {
      newName: "",
      showDialog: false, //编辑姓名弹窗
      showPopup: false, //编辑性别弹窗
      //时间选择
      showBirthday: false,
      minDate: new Date(1994, 8, 24),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      // 图片裁切
      img: "",
      showCropper: false,
    };
  },
  methods: {
    eaitClick() {
      // 点击弹出输入框
      this.showDialog = true;
      // 输入框渲染初始name
      this.newName = this.userInfo.name;
      //自动聚焦  由于vue加载数据是异步操作 所以需要开启一个定时器
      this.$nextTick(() => {
        this.$refs.name.focus();
      });
    },
    async patchEait() {
      let res = await postUser({ name: this.newName });
      console.log(res);
      // 轻弹窗提示修改成功
      this.$toast.success("更改成功");
      // 更新vue数据
      this.$store.commit("userInfodata", {
        //传入原始数据
        ...this.userInfo,
        // 再修改数据
        name: res.data.data.name,
      });
    },
    async sexClick(gender) {
      let res = await postUser({ gender });
      console.log(res);
      // 轻弹窗提示修改成功
      this.$toast.success("更改成功");
      //更新性别数据
      this.$store.commit("userInfodata", {
        // 传入原始数据
        ...this.userInfo,
        gender: res.data.data.gender == 1 ? 1 : 0,
      });
      this.showPopup = false;
    },
    async postBirthday(value) {
      let birthday = moment(value).format("YYYY-MM-DD");
      // console.log(birthday);
      let res =await postUser({ birthday });
      // 提示成功
      this.$toast.success("更新成功");
      // 更新vue数据
      this.$store.commit("userInfodata", {
        ...this.userInfo,
        birthday: res.data.data.birthday,
      });
      this.showBirthday = false;
    },
    afterRead(file) {
      //点击后弹出裁切框
      this.showCropper = true;
      //base64编码赋值给img中 绑定数据
      this.img = file.content;
    },
     confirmPhoto() {
      this.$refs.cropper.getCropBlob((blob) => {
        // 用append原生的方式传入头像
        let fd = new FormData();
        // 追加key  value的方式
        fd.append("photo", blob);
        // 发起请求;
        postPhoto(fd).then((res) => {
          // console.log(res);
          // 轻提示成功
          this.$toast.success("修改成功");
          // 更新数据
          this.$store.commit("userInfodata", {
            // 展开原始数据避免更新数据覆盖问题
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
        this.showCropper = false;
      });
    },
  },
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang="less">
.container {
  .info-img {
    // 头像
    .van-image {
      display: block;
      margin: 20px auto;
    }
    // 文件上传遮罩层
    .van-uploader {
      position: absolute;
      top: 80px;
      left: 150px;
      opacity: 0;
    }
  }
  // 裁切功能父盒子覆盖页面
  .Cropper-box {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>